package com.iraeta.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.iraeta.HikConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service(value = "HikRegionApi")
public class HikRegionApi {
    private static final Logger logger = LoggerFactory.getLogger(HikRegionApi.class);

    @Autowired
    private HikConfig hikConfig;

    public final String  ARTEMIS_PATH = "/artemis";

    public String getRootRegion() throws Exception {
        String url = ARTEMIS_PATH + "/api/resource/v1/regions/root";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("treeCode", "0");
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[regions/root]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[regions/root]错误:" + jsonResult.getString("msg"));
        }
        JSONObject jsonResultData = jsonResult.getJSONObject("data");
        return JSONObject.toJSONString(jsonResultData);
    }

    public String getChildrenRegion(String parentIndexCode) throws Exception {
        String url = ARTEMIS_PATH + "/api/irds/v2/region/nodesByParams";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("resourceType", "region");
        jsonBody.put("isSubRegion", true);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 1000);
        jsonBody.put("parentIndexCodes",   new String[]{parentIndexCode});
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[regions/Children]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[regions/Children]错误:" + jsonResult.getString("msg"));
        }
        JSONObject jsonResultData = jsonResult.getJSONObject("data");
        return JSONObject.toJSONString(jsonResultData);
    }
}
